﻿using UnityEngine;
using System.Collections;

public class TileMaperCreater : MonoBehaviour {

	public Vector2 tileSize;

	public string floorPath,ladderPath;

	public int addX = 5;

	private int[] ladderRows,floorRows;

    private Vector2 mapCenter = Vector2.zero;

	// Use this for initialization
	void Start () {
		//CreaterMap(new Vector2(Screen.width / 100,Screen.height/100));
	}

    public Vector2 MapCenterPosition() {
        return mapCenter;
    }
	/// <summary>
	/// 创建地图
	/// </summary>
	/// <param name="size">Size.</param>
	public void CreaterMap(Vector2 size){
		//获取行列,保证行列为单数
		int cols = singularIntFloor( size.x / tileSize.x ) + addX;
		int rows = singularIntFloor( size.y / tileSize.y );

		InitCreaterRows(3,rows);
		//获取材质
		GameObject ladderGo = Resources.Load<GameObject>(ladderPath);
		GameObject floorGo = Resources.Load<GameObject>(floorPath);
		//生成floor与ladder
		int floorIndex = 0,ladderIndex = 0;
		for(int i = 0; i < rows ; i++){
			bool isCreateFloor = false , isCreateLadder = false;
			Transform rowTransform = null;
			if(floorIndex < floorRows.Length && floorRows[floorIndex] == i){
				isCreateFloor =true;
				GameObject rowGObj = new GameObject();
				rowGObj.name = "floor-"+i;
				rowTransform = rowGObj.transform;
				rowTransform.SetParent(transform);
				floorIndex ++;
			}
			if(ladderIndex < ladderRows.Length && ladderRows[ladderIndex] == i){
				isCreateLadder = true;
			}
			for(int j = 0; j < cols ;j++){
				if(isCreateFloor){
					//生成floor
					GameObject floorObj = Instantiate(floorGo,new Vector3(j * tileSize.x, i *tileSize.y,0),Quaternion.identity) as GameObject;
					floorObj.transform.SetParent(rowTransform);

				}
				if( isCreateLadder && ladderIndex < ladderRows.Length){
					//生成ladder
					GameObject ladderObj = Instantiate(ladderGo,new Vector3(( cols - j - addX ) * tileSize.x, ladderRows[ladderIndex] *tileSize.y,0),Quaternion.identity) as GameObject;
					ladderObj.transform.SetParent(transform);
					ladderIndex ++;
				}
			}
		}
		//将地图中心移动到原点
		transform.position = new Vector3(- ( cols / 2 - 1 ) * tileSize.x,- ( rows / 2 - 1 ) * tileSize.y,0);
		mapCenter.x = ( cols / 2 ) * tileSize.x + transform.position.x;
		mapCenter.y = ( rows / 2 ) * tileSize.y + transform.position.y;
	}
	/// <summary>
	/// 生成地面所在的行数以及生成连接桥所在行数
	/// </summary>
	/// <returns>The creater rows.</returns>
	/// <param name="tileNum">Tile number.</param>
	/// <param name="rows">Rows.</param>
	/// <param name="connection">连接点生成位置.</param>
	void InitCreaterRows(int tileNum,int rows){
		floorRows = new int[tileNum];
		ladderRows = new int[tileNum-1];
		int floorIndex = 0,laderIndex = 0;
		for(int i = 0 ; i < rows ;i++){
			if(floorIndex >= tileNum)
				break;
			if(i % 2 == 0){
				floorRows[floorIndex] = i;
				floorIndex ++;
			}else{
				ladderRows[laderIndex] = i;
				laderIndex ++ ;
			}
		}
	}
	/// <summary>
	/// 保证单数
	/// </summary>
	/// <returns>The int floor.</returns>
	/// <param name="num">Number.</param>
	int singularIntFloor(float num){
		int  sn = Mathf.FloorToInt( num );
		if(	sn % 2 == 0 ){
			sn ++;
		}
		return sn;
	}
}
